/*
 * ConsultaManualPage.java
 *
 * Created on August 15, 2007, 10:58 AM
 * Copyright FSanchez
 */
package consultasmanualviaweb;

import com.computec.manualesweb.constantes.ConstantesGenerales;
import com.computec.manualesweb.delegate.ManualWebDelegate;
import com.computec.manualesweb.exception.ManualWebException;
import com.computec.manualesweb.source.ManualesWebSource;
import com.src.computec.radar.services.utilities.actualizaciones.delegate.ActualizacionesDelegate;
import com.src.computec.radar.services.utilities.dto.FileManagerDto;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import java.sql.Timestamp;
import java.util.ArrayList;
import javax.faces.FacesException;
import com.sun.rave.web.ui.component.Form;
import com.sun.rave.web.ui.component.ThemeLinks;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.UIColumn;
import com.sun.rave.web.ui.component.Alert;
import javax.faces.component.html.HtmlInputHidden;
import javax.faces.context.FacesContext;
import com.sun.rave.web.ui.component.Checkbox;
import java.util.List;
import javax.faces.component.html.HtmlPanelGroup;


public class ConsultaManualPage extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Definition">
    
    private int __placeholder;
    
    private ThemeLinks themeLinks1 = new ThemeLinks();
    
    public ThemeLinks getThemeLinks1() {
	return themeLinks1;
    }
    
    public void setThemeLinks1(ThemeLinks t) {
	this.themeLinks1 = t;
    }
    
    private Form formConsulta = new Form();
    
    public Form getFormConsulta() {
	return formConsulta;
    }
    
    public void setFormConsulta(Form f) {
	this.formConsulta = f;
    }

    private HtmlPanelGrid gridPanelContainer = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelContainer() {
        return gridPanelContainer;
    }

    public void setGridPanelContainer(HtmlPanelGrid hpg) {
        this.gridPanelContainer = hpg;
    }

    private HtmlPanelGrid gridPanelTop = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelTop() {
        return gridPanelTop;
    }

    public void setGridPanelTop(HtmlPanelGrid hpg) {
        this.gridPanelTop = hpg;
    }

    private HtmlPanelGrid gridPanelBottom = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelBottom() {
        return gridPanelBottom;
    }

    public void setGridPanelBottom(HtmlPanelGrid hpg) {
        this.gridPanelBottom = hpg;
    }

    private HtmlOutputText outputTextBusqueda = new HtmlOutputText();

    public HtmlOutputText getOutputTextBusqueda() {
        return outputTextBusqueda;
    }

    public void setOutputTextBusqueda(HtmlOutputText hot) {
        this.outputTextBusqueda = hot;
    }

    private HtmlInputText campoBusqueda = new HtmlInputText();

    public HtmlInputText getCampoBusqueda() {
        return campoBusqueda;
    }

    public void setCampoBusqueda(HtmlInputText hit) {
        this.campoBusqueda = hit;
    }

    private HtmlCommandButton buttonConsultar = new HtmlCommandButton();

    public HtmlCommandButton getButtonConsultar() {
        return buttonConsultar;
    }

    public void setButtonConsultar(HtmlCommandButton hcb) {
        this.buttonConsultar = hcb;
    }

    private HtmlCommandButton buttonEliminar = new HtmlCommandButton();

    public HtmlCommandButton getButtonEliminar() {
        return buttonEliminar;
    }

    public void setButtonEliminar(HtmlCommandButton hcb) {
        this.buttonEliminar = hcb;
    }

    private HtmlCommandButton buttonLimpiar = new HtmlCommandButton();

    public HtmlCommandButton getButtonLimpiar() {
        return buttonLimpiar;
    }

    public void setButtonLimpiar(HtmlCommandButton hcb) {
        this.buttonLimpiar = hcb;
    }

    private HtmlDataTable dataTableManuales = new HtmlDataTable();

    public HtmlDataTable getDataTableManuales() {
        return dataTableManuales;
    }

    public void setDataTableManuales(HtmlDataTable hdt) {
        this.dataTableManuales = hdt;
    }

    private UIColumn nombreManual = new UIColumn();

    public UIColumn getNombreManual() {
        return nombreManual;
    }

    public void setNombreManual(UIColumn uic) {
        this.nombreManual = uic;
    }

    private HtmlOutputText outputText2 = new HtmlOutputText();

    public HtmlOutputText getOutputText2() {
        return outputText2;
    }

    public void setOutputText2(HtmlOutputText hot) {
        this.outputText2 = hot;
    }

    private HtmlOutputText outputText3 = new HtmlOutputText();

    public HtmlOutputText getOutputText3() {
        return outputText3;
    }

    public void setOutputText3(HtmlOutputText hot) {
        this.outputText3 = hot;
    }

    private UIColumn descripcion = new UIColumn();

    public UIColumn getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(UIColumn uic) {
        this.descripcion = uic;
    }

    private HtmlOutputText outputText4 = new HtmlOutputText();

    public HtmlOutputText getOutputText4() {
        return outputText4;
    }

    public void setOutputText4(HtmlOutputText hot) {
        this.outputText4 = hot;
    }

    private HtmlOutputText outputText5 = new HtmlOutputText();

    public HtmlOutputText getOutputText5() {
        return outputText5;
    }

    public void setOutputText5(HtmlOutputText hot) {
        this.outputText5 = hot;
    }

    private UIColumn file = new UIColumn();

    public UIColumn getFile() {
        return file;
    }

    public void setFile(UIColumn uic) {
        this.file = uic;
    }

    private HtmlOutputText outputText7 = new HtmlOutputText();

    public HtmlOutputText getOutputText7() {
        return outputText7;
    }

    public void setOutputText7(HtmlOutputText hot) {
        this.outputText7 = hot;
    }

    private HtmlCommandButton buttonCargar = new HtmlCommandButton();

    public HtmlCommandButton getButtonCargar() {
        return buttonCargar;
    }

    public void setButtonCargar(HtmlCommandButton hcb) {
        this.buttonCargar = hcb;
    }

    private UIColumn version = new UIColumn();

    public UIColumn getVersion() {
        return version;
    }

    public void setVersion(UIColumn uic) {
        this.version = uic;
    }

    private HtmlOutputText outputText1 = new HtmlOutputText();

    public HtmlOutputText getOutputText1() {
        return outputText1;
    }

    public void setOutputText1(HtmlOutputText hot) {
        this.outputText1 = hot;
    }

    private UIColumn checkEliminar = new UIColumn();

    public UIColumn getCheckEliminar() {
        return checkEliminar;
    }

    public void setCheckEliminar(UIColumn uic) {
        this.checkEliminar = uic;
    }

    private HtmlOutputText outputText8 = new HtmlOutputText();

    public HtmlOutputText getOutputText8() {
        return outputText8;
    }

    public void setOutputText8(HtmlOutputText hot) {
        this.outputText8 = hot;
    }

    private Alert alertMessage = new Alert();

    public Alert getAlertMessage() {
        return alertMessage;
    }

    public void setAlertMessage(Alert a) {
        this.alertMessage = a;
    }

    private HtmlInputHidden hiddenField1 = new HtmlInputHidden();

    public HtmlInputHidden getHiddenField1() {
        return hiddenField1;
    }

    public void setHiddenField1(HtmlInputHidden hih) {
        this.hiddenField1 = hih;
    }

    private Checkbox checkboxEliminar = new Checkbox();

    public Checkbox getCheckboxEliminar() {
        return checkboxEliminar;
    }

    public void setCheckboxEliminar(Checkbox c) {
        this.checkboxEliminar = c;
    }

    private HtmlPanelGroup groupPanel1 = new HtmlPanelGroup();

    public HtmlPanelGroup getGroupPanel1() {
        return groupPanel1;
    }

    public void setGroupPanel1(HtmlPanelGroup hpg) {
        this.groupPanel1 = hpg;
    }

    private HtmlCommandButton dataTableManualesHeaderFirstButton = new HtmlCommandButton();

    public HtmlCommandButton getDataTableManualesHeaderFirstButton() {
        return dataTableManualesHeaderFirstButton;
    }

    public void setDataTableManualesHeaderFirstButton(HtmlCommandButton hcb) {
        this.dataTableManualesHeaderFirstButton = hcb;
    }

    private HtmlCommandButton dataTableManualesHeaderPreviousButton = new HtmlCommandButton();

    public HtmlCommandButton getDataTableManualesHeaderPreviousButton() {
        return dataTableManualesHeaderPreviousButton;
    }

    public void setDataTableManualesHeaderPreviousButton(HtmlCommandButton hcb) {
        this.dataTableManualesHeaderPreviousButton = hcb;
    }

    private HtmlCommandButton dataTableManualesHeaderNextButton = new HtmlCommandButton();

    public HtmlCommandButton getDataTableManualesHeaderNextButton() {
        return dataTableManualesHeaderNextButton;
    }

    public void setDataTableManualesHeaderNextButton(HtmlCommandButton hcb) {
        this.dataTableManualesHeaderNextButton = hcb;
    }

    private HtmlCommandButton dataTableManualesHeaderLastButton = new HtmlCommandButton();

    public HtmlCommandButton getDataTableManualesHeaderLastButton() {
        return dataTableManualesHeaderLastButton;
    }

    public void setDataTableManualesHeaderLastButton(HtmlCommandButton hcb) {
        this.dataTableManualesHeaderLastButton = hcb;
    }
    
    // </editor-fold>
    public ConsultaManualPage() {
	// <editor-fold defaultstate="collapsed" desc="Creator-managed Component Initialization">
	try {
	} catch (Exception e) {
	    log("Page1 Initialization Failure", e);
	    throw e instanceof javax.faces.FacesException ? (FacesException) e: new FacesException(e);
	}
	// </editor-fold>
	// TODO: Add your own initialization code here (optional)
    }

    /** 
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected RequestManualesViaWebBean getRequestManualesViaWebBean() {
        return (RequestManualesViaWebBean)getBean("RequestManualesViaWebBean");
    }


    /** 
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected ApplicationManualesViaWebBean getApplicationManualesViaWebBean() {
        return (ApplicationManualesViaWebBean)getBean("ApplicationManualesViaWebBean");
    }


    /** 
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected SessionManualesViaWebBean getSessionManualesViaWebBean() {
        return (SessionManualesViaWebBean)getBean("SessionManualesViaWebBean");
    }


    /** 
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();	
	
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("PortletPage1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
	if(this.getSessionManualesViaWebBean().getListManuales()==null ){
	    this.buscarManualesInit("");	     	  
	}
    }

    /** 
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    public void preprocess() {
    }

    /** 
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    public void prerender() {
    }

    /** 
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    public void destroy() {
    }

    /** 
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }


    public String buttonCargar_action() {
        return "CARGAR";
    }


    public void buttonConsultar_action() {
	this.getSessionManualesViaWebBean().setRenderedAlertConsulta(false);	
	String param_=(String)this.campoBusqueda.getValue();
	this.buscarManuales(param_);
     
    }
    private void buscarManuales(String pStr){	
	ArrayList list_=(ArrayList)this.getSessionManualesViaWebBean().getManualesRadar(pStr.toUpperCase());
	if(list_.size()>0){
	    this.getSessionManualesViaWebBean().setListManuales(list_);
	    this.getSessionManualesViaWebBean().setRenderedTabla(true);
	}else{
	    this.getSessionManualesViaWebBean().setRenderedTabla(false);
	    this.getSessionManualesViaWebBean().setRenderedAlertConsulta(true);		
	    this.setMensajeAlert("warning", "Advertencia", ManualesWebSource.getInstance().getManualesWebErrorSetupKey("NoExistenManuales"), true);
	}
    }
    
   private void buscarManualesInit(String pStr){	
	ArrayList list_=(ArrayList)this.getSessionManualesViaWebBean().getManualesRadar(pStr.toUpperCase());
	if(list_.size()>0){
	    this.getSessionManualesViaWebBean().setListManuales(list_);
	    this.getSessionManualesViaWebBean().setRenderedTabla(true);
	}else{
	    this.getSessionManualesViaWebBean().setRenderedTabla(false);
//	    this.getSessionManualesViaWebBean().setRenderedAlertConsulta(true);		
//	    this.setMensajeAlert("warning", "Advertencia", ManualesWebSource.getInstance().getManualesWebErrorSetupKey("NoExistenManuales"), true);
	}
    }
    public void setMensajeAlert(String type, String summary, String detail, boolean visible) 
    {
        if (visible){
            this.alertMessage.setType(type);
            this.alertMessage.setSummary(summary);
            this.alertMessage.setDetail(detail);
            this.alertMessage.setVisible(visible);
        }else{
            this.alertMessage.setVisible(visible);
        }
    }


    public void buttonLimpiar_action() {
       campoBusqueda.setValue("");
       this.getSessionManualesViaWebBean().setRenderedAlertConsulta(false);
       this.getSessionManualesViaWebBean().setRenderedTabla(false);
    }
    

    public void linkActionFileDownload_action() 
    {
        try
        {
	    
            String relativePath_=(String)this.hiddenField1.getValue();
            String fileDownloaderPath =this.getApplicationManualesViaWebBean().getFileDownloaderPath();            
            String idUser = this.getSessionManualesViaWebBean().getLdapUser();            
            long longCurrentTimeMillis = System.currentTimeMillis();
            
            com.src.computec.radar.services.base.constantes.ConstantesGenerales constantesServices = com.src.computec.radar.services.base.constantes.ConstantesGenerales.getInstance();
            ConstantesGenerales constantesManuales = ConstantesGenerales.getInstance();
            
            FileManagerDto fileManagerDto = new FileManagerDto();
            fileManagerDto.setIdDescarga(new Timestamp(longCurrentTimeMillis));
            fileManagerDto.setCodigoResponsable(idUser);
            fileManagerDto.setOrigen(constantesManuales.FILE_ORIGEN_MANUAL_VIA_WEB);
            fileManagerDto.setRutaArchivo(relativePath_);//   /Manuales/nombrearchivo
            fileManagerDto.setEstado(constantesServices.ACTIVE_STATE);
            
            ActualizacionesDelegate actualizacionesDelegate = new ActualizacionesDelegate();
            actualizacionesDelegate.insertFileManager(fileManagerDto);
            
            FacesContext.getCurrentInstance().getExternalContext().redirect(fileDownloaderPath + longCurrentTimeMillis);    
            FacesContext.getCurrentInstance().renderResponse();     
        }
        catch (Exception e) 
        {
            e.printStackTrace();
            this.setMensajeAlert("error", "Error",ManualesWebSource.getInstance().getManualesWebErrorSetupKey("ErrorSistema"), true);
            this.getSessionManualesViaWebBean().setRenderedAlertConsulta(true);
        }     
    }


    public String buttonEliminar_action() {
     
       this.getSessionManualesViaWebBean().setRenderedAlertConsulta(false);	
        try 
        {
            if( checkboxEliminar.getSelected("seleccionGroup") != null )
            {
                List listManualesSelected = checkboxEliminar.getSelected("seleccionGroup");
		
		if(listManualesSelected.size()>0){

                  ManualWebDelegate delegate =new ManualWebDelegate();
		  int ret_=delegate.deleteManual(listManualesSelected);
		  
		 if(ret_==listManualesSelected.size()){
		    this.buscarManuales("");
		    this.setMensajeAlert("success", "Exito", ManualesWebSource.getInstance().getManualesWebSetupKey("SuccessManualDelete"), true);
		    this.getSessionManualesViaWebBean().setRenderedAlertConsulta(true);	
		 }else{
		        this.setMensajeAlert("error", "Error",ManualesWebSource.getInstance().getManualesWebErrorSetupKey("ErrorSistema"), true);
			this.getSessionManualesViaWebBean().setRenderedAlertConsulta(true);
		 }
	      }else{
		    this.getSessionManualesViaWebBean().setRenderedModificarButton(true);
		    this.setMensajeAlert("warning", "Advertencia", ManualesWebSource.getInstance().getManualesWebErrorSetupKey("WarningDeleteManual"), true);	
	      }
	    }
        }
        catch (ManualWebException e) {
            e.printStackTrace();
            this.setMensajeAlert("error", "Error",ManualesWebSource.getInstance().getManualesWebErrorSetupKey("ErrorSistema"), true);
            this.getSessionManualesViaWebBean().setRenderedAlertConsulta(true);
        }                     
        return null;
    }


    public String dataTableManuales_firstPageAction() {
        dataTableManuales.setFirst(0);
        return null;
    }


    public String dataTableManuales_previousPageAction() {
        int first = dataTableManuales.getFirst() - dataTableManuales.getRows();
        if (first < 0) {
            first = 0;
        }
        dataTableManuales.setFirst(first);
        return null;
    }


    public String dataTableManuales_nextPageAction() {
        int first = dataTableManuales.getFirst() + dataTableManuales.getRows();
        dataTableManuales.setRowIndex(first);
        if (dataTableManuales.isRowAvailable()) {
            dataTableManuales.setFirst(first);
        }
        return null;
    }


    public String dataTableManuales_lastPageAction() {
        int first = dataTableManuales.getFirst();
        while (true) {
            dataTableManuales.setRowIndex(first + 1);
            if (dataTableManuales.isRowAvailable()) {
                first++;
            } else {
                break;
            }
        }
        dataTableManuales.setFirst(first - (first % dataTableManuales.getRows()));
        return null;
    }
}
